<?php
include_once("admin.inc.php");
$comModel = new Model("comment");
$proModel = new Model("product");
$do = get_param("do");
switch( $do ){
	case "comments":
		$list = $comModel->order('id DESC')->get();
		$product = $proModel->get();
		foreach( $product as $v_p ){
			foreach( $list as $k=>$v ){
				if( $v_p['id']==$v['p_id'] ){
					$list[$k]['p_name'] = $v_p['name'];
				}
			}
		}
		$smarty->assign('list', $list);
		$smarty->display('admin/comments.html'); 
	break;
	case "addCom":
		$pid = intval(get_param("pid"));
		if( get_param("submit") ){
			$data = get_data();	
			if( empty($data['user']) || empty($data['words']) || empty($data['p_id']) ){
				msg('必填字段不能为空',-1);
			}
			$data['addtime'] = time();
			$data['words'] = htmlspecialchars($data['words']);
			unset($data['id']);
			$rs = $comModel->value($data)->add();
			if( $rs>0 ){
				msg('添加成功', 'comment.php?do=comments');
			}else{
				msg('添加失败', -1);
			}
			exit; 
		}
		$product = $proModel->get();
		$smarty->assign("product", $product);
		$smarty->assign('pid',$pid);
		$smarty->display("admin/addComment.html");
	break;
	case "delCom":
		$id = intval(get_param('id'));
		if( empty($id) ) msg('非法ID',-1);
		if( $comModel->where("id=$id")->del() ){
			ajax_return(array('status'=>'success', 'info'=>'删除成功'));
		}else{
			ajax_return(array('status'=>'failed', 'info'=>'删除失败'));
		}
	break;
	case 'editCom':
		$id = intval(get_param('id'));
		if( empty($id) ) msg('非法ID',-1);
		if( get_param('submit') ){
			$data = get_data();
			if( empty($data['user']) || empty($data['words']) || empty($data['p_id']) ){
				msg('必填字段不能为空',-1);
			}
			$data['addtime'] = time();
			$data['words'] = htmlspecialchars($data['words']);
			$rs = $comModel->where("id=$id")->value($data)->update();
			if( $rs ){
				msg('修改成功','comment.php?do=comments');
			}else{
				msg('修改失败');
			}
		
		}
		$cominfo = $comModel->where("id=$id")->get_one();
		$smarty->assign('cominfo', $cominfo);
		$smarty->assign('pid', $cominfo['p_id']);
		$product = $proModel->get();
		$smarty->assign('product', $product);
		$smarty->assign("pageTitle", '修改评论');
		$smarty->assign('do', 'editCom');
		$smarty->display('admin/addComment.html');
	break;
}
